//
//  EMMSalesTargetViewController.m
//  EMMApp
//
//  Created by Mac on 16/7/8.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMSalesTargetViewController.h"
#import "TableViewCell.h"
#import "GetSalesTargetListApi.h"
#import "SNChart.h"

@interface EMMSalesTargetViewController ()<UITableViewDelegate,UITableViewDataSource,SNChartDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (strong, nonatomic) UIView *headBackView;
@property (strong, nonatomic) UIImageView *headImageview;
@property (strong, nonatomic)UIButton *djBtn;
@property (strong, nonatomic)UIButton *btn;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) NSMutableArray *da1;
@property (nonatomic, strong) NSMutableArray *da2;
@property (nonatomic, strong) NSMutableArray *daName1;
@property (nonatomic, strong) NSMutableArray *daName2;
@property (nonatomic, strong) NSMutableArray *daNumOne1;
@property (nonatomic, strong) NSMutableArray *daNumOne2;
@property (nonatomic, strong) NSMutableArray *daNumTwo1;
@property (nonatomic, strong) NSMutableArray *daNumTwo2;

@property (nonatomic, strong) SNChart *c1;

@property (nonatomic, strong) SNChart *c2;

@property (nonatomic, strong) SNChart *c3;

@property (nonatomic, strong) SNChart *c4;


@end

@implementation EMMSalesTargetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationController];
    [self creatDropDownView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-40) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 导航

- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"销售指标";
    [self configLeftBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _da1 = [[NSMutableArray alloc] init];
    _da2 = [[NSMutableArray alloc] init];
    _daName1 = [[NSMutableArray alloc] init];
    _daName2 = [[NSMutableArray alloc] init];
    _daNumOne1 = [[NSMutableArray alloc] init];
    _daNumOne2 = [[NSMutableArray alloc] init];
    _daNumTwo1 = [[NSMutableArray alloc] init];
    _daNumTwo2 = [[NSMutableArray alloc] init];
    [self showHudInWindow:@"正在加载请稍等..."];
    [self postGetSalesTargetListApiTargetType:@"1"];
}


- (void)postGetSalesTargetListApiTargetType:(NSString *)targetType {
    [_da1 removeAllObjects];
    [_da2 removeAllObjects];
    [_daName1 removeAllObjects];
    [_daName2 removeAllObjects];
    [_daNumOne1 removeAllObjects];
    [_daNumOne2 removeAllObjects];
    [_daNumTwo1 removeAllObjects];
    [_daNumTwo2 removeAllObjects];
    GetSalesTargetListApi *api = [[GetSalesTargetListApi alloc] initWithTargetType:targetType];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetSalesTargetListApi *result = (GetSalesTargetListApi *)request;
        if (result.isSuccess) {
            GetSalesTargetList *get = result.resultData;
            for (MonthTargetList *list in get.monthTargetList) {
                [_da1 addObject:list];
                [_daNumOne1 addObject:list.targetNum];
            }
            for (TargetCompleteList *list in get.targetCompleteList) {
                [_daNumOne2 addObject:list.completeNum];
            }
            for (MonthTotalTargetList *list in get.monthTotalTargetList) {
                [_da2 addObject:list];
                [_daNumTwo1 addObject:list.mt_targetNum];
            }
            for (MonthTotalCompletetList *list in get.monthTotalCompletetList) {
                [_daNumTwo2 addObject:list.mt_completeNum];
            }

        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [_tableView reloadData];
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        
        [self hideHud];
    }];
}


#pragma mark - 跳出层初始化
- (void)creatDropDownView {//
    self.headBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 40)];
    self.headBackView.backgroundColor = [UIColor colorFromHexCode:@"##fff5e9"];
    [self.view addSubview:self.headBackView];
    _djBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _djBtn.frame = CGRectMake(0, 5, EMM_WIDTH/2, 32);
    _djBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_djBtn setTitle:@"销售指标" forState:UIControlStateNormal];
    [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#ff7200"] forState:UIControlStateNormal];
    _djBtn.tag = 2001;
    [_djBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _djBtn.titleLabel.bounds.size.width+EMM_WIDTH/2-20, 0, _djBtn.titleLabel.bounds.size.width)];
    NSLog(@"%f",_djBtn.titleLabel.bounds.size.width);
    [_djBtn addTarget:self action:@selector(headButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBackView addSubview:_djBtn];
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(EMM_WIDTH/2, 5, EMM_WIDTH/2, 32);
    _btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_btn setTitle:@"资金回笼指标" forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _btn.tag = 2002;
    [_btn setImageEdgeInsets:UIEdgeInsetsMake(0, _btn.titleLabel.bounds.size.width+EMM_WIDTH-40, 0, _btn.titleLabel.bounds.size.width)];
    [_btn addTarget:self action:@selector(headButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBackView addSubview:_btn];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH/2, 5, 1, 30)];
    label.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [self.headBackView addSubview:label];
    _headImageview = [[UIImageView alloc] initWithFrame:CGRectMake((EMM_WIDTH/2-70)/2, 38, 70, 2)];
    _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [self.headBackView addSubview:self.headImageview];
}

- (void)setScrollView:(UIScrollView *)scrollView width:(CGFloat)width {
    
    scrollView.showsHorizontalScrollIndicator = NO;
    
    scrollView.showsVerticalScrollIndicator = NO;
    
    scrollView.pagingEnabled = YES;
    
    scrollView.bounces = NO;
    
    scrollView.contentSize = CGSizeMake(width,scrollView.bounds.size.height);
    
}


#pragma mark - 顶部按键响应事件
- (void)headButtonClick:(UIButton *)button {
    [self headLineViewScrollWithIndex:button.tag-2001];
    [UIView animateWithDuration:0.5 animations:^{
        _mainScrollView.contentOffset = CGPointMake((button.tag-2001)*ScreenSize.width, 0);
    }];
    
    if (button.tag == 2001) {
        [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#ff7200"] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self showHudInWindow:@"正在加载请稍等..."];
        [self postGetSalesTargetListApiTargetType:@"1"];
    }else if (button.tag == 2002) {
        [_djBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor colorFromHexCode:@"#ff7200"] forState:UIControlStateNormal];
        [self showHudInWindow:@"正在加载请稍等..."];
        [self postGetSalesTargetListApiTargetType:@"2"];
    }
    
}

#pragma mark - 顶部线移动,
- (void)headLineViewScrollWithIndex:(NSInteger)index {
    [UIView animateWithDuration:0.15 animations:^{
        if (index == 0) {
            self.headImageview.frame = CGRectMake((EMM_WIDTH/2-70)/2, 38, 70, 2);
        }else if (index == 1) {
            self.headImageview.frame = CGRectMake((EMM_WIDTH/2-70)/2+EMM_WIDTH/2, 38, 70, 2);
        }
    }];
}

#pragma mark - UITableView代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 400;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableViewCellId";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 399, EMM_WIDTH, 1)];
    imageView.backgroundColor = [UIColor colorFromHexCode:@"#bebebe"];
    [cell addSubview:imageView];
//    [cell configUI:indexPath];
    NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:
                            @"  1月  ",@"  2月  ",@"3月",@"4月",
                            @"5月",@"6月",@"7月",@"8月",
                            @"9月",@"10月",@"11月",@"12月", nil];
    //NSMutableArray *ary2 = [NSMutableArray arrayWithObjects:@"76",@"34",@"54",@"23",@"16",@"32",@"17", nil];
//    NSMutableArray *ary3 = [NSMutableArray arrayWithObjects:@"3", @"12",@"25",@"55",@"52",@"24",@"85", nil];
//    NSMutableArray *ary4 = [NSMutableArray arrayWithObjects:@"23",@"42",@"25",@"15",@"30",@"42",@"32", nil];
//    NSMutableArray *ary5 = [NSMutableArray arrayWithObjects:@"3", @"12",@"25",@"55",@"52",@"24",@"85", nil];
    
    if (indexPath.row == 0) {
        [cell initWithXNumArr:arr1 oneNumArr:_daNumOne1 twoNumArr:_daNumOne2 oneNumArr1:_daNumTwo1 twoNumArr1:_daNumTwo2 indexPath:indexPath];
    }else{
        [cell initWithXNumArr:arr1 oneNumArr:_daNumTwo1 twoNumArr:_daNumTwo2 oneNumArr1:_daNumTwo1 twoNumArr1:_daNumTwo2 indexPath:indexPath];
    }
    
    if (indexPath.row == 0) {
        UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 160, 20)];
        la1.font = [UIFont systemFontOfSize:15];
        la1.text = @"月指标完成情况";
        la1.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:la1];
        
        UILabel *la2 = [[UILabel alloc] initWithFrame:CGRectMake(190, 10, 100, 20)];
        la2.font = [UIFont systemFontOfSize:15];
        la2.text = @"单位/万元";
        la2.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:la2];
        
        
        
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 40, 1)];
        v1.backgroundColor = [UIColor colorFromHexCode:@"#143963"];
        [cell addSubview:v1];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30+(40-8)/2, 47, 8, 8)];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 4;
        view.backgroundColor = [UIColor colorFromHexCode:@"#143963"];
        [cell addSubview:view];
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, 60, 20)];
        label1.font = [UIFont systemFontOfSize:15];
        label1.text = @"指标";
        label1.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:label1];
        
        
        UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(150, 50, 40, 1)];
        v2.backgroundColor = [UIColor colorFromHexCode:@"#a9575d"];
        [cell addSubview:v2];
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(150+ (40-8)/2, 47, 8, 8)];
        view2.layer.masksToBounds = YES;
        view2.layer.cornerRadius = 4;
        view2.backgroundColor = [UIColor colorFromHexCode:@"#a9575d"];
        [cell addSubview:view2];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 40, 100, 20)];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"完成情况";
        label2.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:label2];
//        _c1 = [[SNChart alloc] initWithFrame:CGRectMake(0, 80, EMM_WIDTH, 350) withDataSource:self andChatStyle:SNChartStyleLine];
//        [_c1 showInView:cell];
//        _c2 = [[SNChart alloc] initWithFrame:CGRectMake(0, 80, EMM_WIDTH, 350) withDataSource:self andChatStyle:SNChartStyleLine];
//        [_c2 showInView:cell];
    }else {
        imageView.hidden = YES;
        UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 160, 20)];
        la1.font = [UIFont systemFontOfSize:15];
        la1.text = @"累计月指标完成情况";
        la1.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:la1];
        
        UILabel *la2 = [[UILabel alloc] initWithFrame:CGRectMake(190, 10, 100, 20)];
        la2.font = [UIFont systemFontOfSize:15];
        la2.text = @"单位/万元";
        la2.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:la2];
        
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 40, 1)];
        v1.backgroundColor = [UIColor colorFromHexCode:@"#143963"];
        [cell addSubview:v1];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30+(40-8)/2, 47, 8, 8)];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 4;
        view.backgroundColor = [UIColor colorFromHexCode:@"#143963"];
        [cell addSubview:view];
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, 60, 20)];
        label1.font = [UIFont systemFontOfSize:15];
        label1.text = @"指标";
        label1.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:label1];
        
        
        UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(150, 50, 40, 1)];
        v2.backgroundColor = [UIColor colorFromHexCode:@"#a9575d"];
        [cell addSubview:v2];
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(150+ (40-8)/2, 47, 8, 8)];
        view2.layer.masksToBounds = YES;
        view2.layer.cornerRadius = 4;
        view2.backgroundColor = [UIColor colorFromHexCode:@"#a9575d"];
        [cell addSubview:view2];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 40, 100, 20)];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"完成情况";
        label2.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:label2];
//        
//        _c3 = [[SNChart alloc] initWithFrame:CGRectMake(0, 80, EMM_WIDTH, 350) withDataSource:self andChatStyle:SNChartStyleLine];
//        [_c3 showInView:cell];
//        
//        _c4 = [[SNChart alloc] initWithFrame:CGRectMake(0, 80, EMM_WIDTH, 350) withDataSource:self andChatStyle:SNChartStyleLine];
//        [_c4 showInView:cell];
    }
    return cell;
}

//[cell initWithXNumArr:arr1 oneNumArr:_daNumOne1 twoNumArr:_daNumOne2 oneNumArr1:_daNumTwo1 twoNumArr1:_daNumTwo2 indexPath:indexPath];
//- (NSArray *)chatConfigYValue:(SNChart *)chart {
//    //    return @[@"0.1",@"0.5",@"0.70",@"0.30",@"0.50",@"0.014",@"0",@"0.14"];
//    //    return @[@"100",@"50",@"70",@"30",@"50",@"14",@"5",@"14",@"5",@"14"];
//    if (chart == _c2) {
//        return _daNumOne2;
//    }else if (chart == _c3) {
//        return _daNumTwo1;
//    }else if (chart == _c4) {
//        return _daNumTwo2;
//    }
//    NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月", nil];
//
//    return arr1;
////    return _daNumOne1;
//
//}
//
//- (NSArray *)chatConfigXValue:(SNChart *)chart {
//    //    return @[@"12-1-2016",@"12-2-2016",@"12-3-2016",@"12-4-2016",@"12-5-2016",@"12-6-2016",@"12-7-2016",@"12-8-2016",@"12-9-2016",@"12-10-2016"];
//    NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月", nil];
//
//    return arr1;
//}

@end
